﻿using System.Collections.Generic;
using BlackBox;
using NUnit.Framework;

namespace Tests.Unit
{
    [TestFixture]
    public class WhenMakingBuySellDecision
    {
        [Test]
        public void PercentageChangeUnderThresholdTriggersNoOp()
        {
            var decisionEngine = new DecisionEngine( 0.001m );
            var analysisData = new AnalysisData
                                   {
                                       NewsId = 0,
                                       MatchingNewsId = 0,
                                       ActualChangePercentage = new Dictionary<MinuteInterval, decimal>
                                                                    {
                                                                        {MinuteInterval.One, 0m},
                                                                        {MinuteInterval.Three, 0m},
                                                                        {MinuteInterval.Five, 0m},
                                                                        {MinuteInterval.Ten, 0m},
                                                                        {MinuteInterval.Twenty, 0m},
                                                                        {MinuteInterval.FortyFive, 0m}
                                                                    },
                                       ProjectedChangePercentage = new Dictionary<MinuteInterval, decimal>
                                                                       {
                                                                           {MinuteInterval.One, 0m},
                                                                           {MinuteInterval.Three, 0m},
                                                                           {MinuteInterval.Five, 0m},
                                                                           {MinuteInterval.Ten, 0m},
                                                                           {MinuteInterval.Twenty, 0m},
                                                                           {MinuteInterval.FortyFive, 0m}
                                                                       },
                                       PriceAtEvent = 1.00m
                                   };
            var action = decisionEngine.MakeDecision( analysisData, MinuteInterval.FortyFive );
            Assert.That( action, Is.EqualTo( MarketAction.None ) );
        }

        [Test]
        public void PercentageChangeAboveBuyThresholdTriggersBuy()
        {
            var decisionEngine = new DecisionEngine( 0.001m );
            var analysisData = new AnalysisData
                                   {
                                       NewsId = 0,
                                       MatchingNewsId = 0,
                                       ActualChangePercentage = new Dictionary<MinuteInterval, decimal>
                                                                    {
                                                                        {MinuteInterval.One, 0m},
                                                                        {MinuteInterval.Three, 0m},
                                                                        {MinuteInterval.Five, 0m},
                                                                        {MinuteInterval.Ten, 0m},
                                                                        {MinuteInterval.Twenty, 0m},
                                                                        {MinuteInterval.FortyFive, 0m}
                                                                    },
                                       ProjectedChangePercentage = new Dictionary<MinuteInterval, decimal>
                                                                       {
                                                                           {MinuteInterval.One, 0m},
                                                                           {MinuteInterval.Three, 0m},
                                                                           {MinuteInterval.Five, 0m},
                                                                           {MinuteInterval.Ten, 0m},
                                                                           {MinuteInterval.Twenty, 0m},
                                                                           {MinuteInterval.FortyFive, 0.02m}
                                                                       },
                                       PriceAtEvent = 1.00m
                                   };

            var action = decisionEngine.MakeDecision( analysisData, MinuteInterval.FortyFive );
            Assert.That( action, Is.EqualTo( MarketAction.Buy ) );
        }
        
        [Test]
        public void PercentageChangeEqualToBuyThresholdTriggersBuy()
        {
            var decisionEngine = new DecisionEngine( 0.001m );
            var analysisData = new AnalysisData
                                   {
                                       NewsId = 0,
                                       MatchingNewsId = 0,
                                       ActualChangePercentage = new Dictionary<MinuteInterval, decimal>
                                                                    {
                                                                        {MinuteInterval.One, 0m},
                                                                        {MinuteInterval.Three, 0m},
                                                                        {MinuteInterval.Five, 0m},
                                                                        {MinuteInterval.Ten, 0m},
                                                                        {MinuteInterval.Twenty, 0m},
                                                                        {MinuteInterval.FortyFive, 0m}
                                                                    },
                                       ProjectedChangePercentage = new Dictionary<MinuteInterval, decimal>
                                                                       {
                                                                           {MinuteInterval.One, 0m},
                                                                           {MinuteInterval.Three, 0m},
                                                                           {MinuteInterval.Five, 0m},
                                                                           {MinuteInterval.Ten, 0m},
                                                                           {MinuteInterval.Twenty, 0m},
                                                                           {MinuteInterval.FortyFive, 0.001m}
                                                                       },
                                       PriceAtEvent = 1.00m
                                   };

            var action = decisionEngine.MakeDecision( analysisData, MinuteInterval.FortyFive );
            Assert.That( action, Is.EqualTo( MarketAction.Buy ) );
        }
        
        [Test]
        public void PercentageChangeBelowSellThresholdTriggersSell()
        {
            var decisionEngine = new DecisionEngine( 0.001m );
            var analysisData = new AnalysisData
                                   {
                                       NewsId = 0,
                                       MatchingNewsId = 0,
                                       ActualChangePercentage = new Dictionary<MinuteInterval, decimal>
                                                                    {
                                                                        {MinuteInterval.One, 0m},
                                                                        {MinuteInterval.Three, 0m},
                                                                        {MinuteInterval.Five, 0m},
                                                                        {MinuteInterval.Ten, 0m},
                                                                        {MinuteInterval.Twenty, 0m},
                                                                        {MinuteInterval.FortyFive, 0m}
                                                                    },
                                       ProjectedChangePercentage = new Dictionary<MinuteInterval, decimal>
                                                                       {
                                                                           {MinuteInterval.One, 0m},
                                                                           {MinuteInterval.Three, 0m},
                                                                           {MinuteInterval.Five, 0m},
                                                                           {MinuteInterval.Ten, 0m},
                                                                           {MinuteInterval.Twenty, 0m},
                                                                           {MinuteInterval.FortyFive, -0.02m}
                                                                       },
                                       PriceAtEvent = 1.00m
                                   };

            var action = decisionEngine.MakeDecision( analysisData, MinuteInterval.FortyFive );
            Assert.That( action, Is.EqualTo( MarketAction.Short ) );
        }
        
        [Test]
        public void PercentageChangeEqualToSellThresholdTriggersSell()
        {
            var decisionEngine = new DecisionEngine( 0.001m );
            var analysisData = new AnalysisData
                                   {
                                       NewsId = 0,
                                       MatchingNewsId = 0,
                                       ActualChangePercentage = new Dictionary<MinuteInterval, decimal>
                                                                    {
                                                                        {MinuteInterval.One, 0m},
                                                                        {MinuteInterval.Three, 0m},
                                                                        {MinuteInterval.Five, 0m},
                                                                        {MinuteInterval.Ten, 0m},
                                                                        {MinuteInterval.Twenty, 0m},
                                                                        {MinuteInterval.FortyFive, 0m}
                                                                    },
                                       ProjectedChangePercentage = new Dictionary<MinuteInterval, decimal>
                                                                       {
                                                                           {MinuteInterval.One, 0m},
                                                                           {MinuteInterval.Three, 0m},
                                                                           {MinuteInterval.Five, 0m},
                                                                           {MinuteInterval.Ten, 0m},
                                                                           {MinuteInterval.Twenty, 0m},
                                                                           {MinuteInterval.FortyFive, -0.001m}
                                                                       },
                                       PriceAtEvent = 1.00m
                                   };

            var action = decisionEngine.MakeDecision( analysisData, MinuteInterval.FortyFive );
            Assert.That( action, Is.EqualTo( MarketAction.Short ) );
        }
    }
}
